<?php
    session_start();
    if(isset($_POST['id'])){
        include("class/Conexion.php");

        if(isset($_POST['page'])){
            $page= $_POST['page'];
        }else{
            $page=1;
        }
        if($_POST['id']==0)
            $sql="SELECT * FROM producto";
        else
            $sql="SELECT * FROM producto where categoria = ".$_POST['id']." ";

        $datos=mysql_query($sql,$conexion);
        $numrows=mysql_num_rows($datos);
        $rows_per_page= 4;
        $lastpage= ceil($numrows / $rows_per_page);

        $page=(int)$page;
        if($page > $lastpage){
            $page= $lastpage;
        }
        if($page < 1){
            $page=1;
        }
        $limit= 'LIMIT '. ($page -1) * $rows_per_page . ',' .$rows_per_page;
        $sql.=" $limit";
        $table=mysql_query($sql,$conexion);
        if(mysql_num_rows($table) != 0){
            while($row = mysql_fetch_array($table)){
                if(isset($_SESSION['items']))
                    $valor=$_SESSION['items'];
                else
                    $valor=0;

                echo '
                    <div class="catalogo" >
                    <div class="goods">
                    <div style="padding:15px 0px 0 8px">
                    <div class="item_zag_main zag" style="text-align:center">
                        <a href="#">'.$row['nombre'].'</a>
                    </div>
                    <div style="float:left;padding:5px 10px 0 10px; width: 88px">
                        <a href="#">
                            <img src="'.$row['img'].'" border="0" alt="Nulla mollis" title=" Nulla mollis " width="88" height="169"/>
                        </a>
                    </div>
                    <div class="item_des" style="padding-top:25px">Existencia:'.$row['cantidad'].'</div>
                    <div style="height:13px"></div>
                    <div class="item_price">$'.$row['precio'].'</div>
                    <div style=" margin:0px 0 0 0px;">
                        <div name="cart_quantity">
                            <input type="image" onClick="refreshItems('.$valor.','.$row['id'].')" src="includes/languages/english/images/buttons/../../../../../images/button_in_cart.gif" border="0" alt="Add to Cart" title=" Add to Cart " style="margin:15px 0px 6px 0px; float:left"/>
                            <input type="hidden" name="new" value="'.$row['id'].'"/>
                        </div>
                        <a id="linkimg" href="javascript:;" title="ver detalles" onClick="fn_popupbox(\'php/detalle.php?id='.$row['id'].'\',\'iframe\');" >
                            <img src="images/button_det.gif" border="0" alt="" width="89" height="24" style="float:left"/>
                        </a>
                    </div>&nbsp;
                </div></div></div>';
            }
        }else{
            echo '<div class="alert" align="center">No se encontro ningun dato</div>';
        }

        echo '<div id ="pagination-clean" style="clear:both;width:606px;">';
        if($numrows != 0){
            $nextpage= $page +1;
            $prevpage= $page -1;
            echo '<ul  id="pagination-digg" >';
            if ($page == 1) {
                echo '<li class="previous-off">&laquo; Anterior</li>
                    <li class="active">1</li>';
                for($i= $page+1; $i<= $lastpage ; $i++){
                    echo '<li><a href="#" onClick="showarticulos('.$_POST['id'].','.$i.')">'.$i.'</a></li>';
                }
                if($lastpage >$page ){
                    echo '<li class="next"><a href="#" onClick="showarticulos('.$_POST['id'].','.$nextpage.')" >Siguiente &raquo;</a></li>';
                }else{
                    echo '<li class="next-off">Siguiente &raquo;</li>';
                }
            } else {
                echo '<li class="previous"><a href="#" onClick="showarticulos('.$_POST['id'].','.$prevpage.')">&laquo; Anterior</a></li>';
                for($i= 1; $i<= $lastpage ; $i++){
                    if($page == $i){
                        echo '<li class="active">'.$i.'</li>';
                    }else{
                        echo '<li><a href="#" onClick="showarticulos('.$_POST['id'].','.$i.')">'.$i.'</a></li>';
                    }
                }
                if($lastpage >$page ){
                    echo '<li class="next"><a href="#" onClick="showarticulos('.$_POST['id'].','.$nextpage.')">Siguiente &raquo;</a></li>';
                }else{
                    echo '<li class="next-off">Siguiente &raquo;</li>';
                }
            }
            echo '</ul></div>';
        }

    }
?>
